home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / DTS QT Utilities.Aug-95 / Projects & Test Apps / DigitizerShell / Mac Framework / MacApplication.c < prev    next >
Encoding:
Text File  |  1995-07-11  |  9.9 KB  |  424 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        MacApplication.c
  3.  
  4.     Contains:    Digitizer Shell specific functions concerning the application shell.
  5.  
  6.     Written by:    DTS
  7.  
  8.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.        <1>         4/25/95        khs        first file
  13.        
  14. */
  15.  
  16.  
  17. // INCLUDES
  18. #include "MacApplication.h"
  19. #include "MacFramework.h"
  20. #include "AppConfiguration.h"
  21. #include "DTSQTUtilities.h"
  22.  
  23. // Header file for the specific test functions.
  24. #include "TestFunctions.h"
  25.  
  26. // GLOBALS
  27. long gMaxMilliSecToUse = 0L;        
  28.  
  29. // These are our global SG settings, if needed we need to export these using get/set functions
  30. // as these are internal to this file (.c).
  31. SeqGrabComponent     gSG = NULL;
  32. WindowPtr                gCaptureWindow = NULL;
  33. SGChannel                    gVideoChannel = NULL;
  34. SGChannel                    gAudioChannel = NULL;
  35. SGGrabCompleteUPP    gSGGrabCompleteUPP = 0;
  36.  
  37. static long                    gGrabCompleteCounter = 0L;
  38. static long                    gGrabCompleteTimer = 0L;
  39.  
  40.  
  41. // FUNCTIONS
  42. // ______________________________________________________________________
  43. void DoIdle(WindowRef theWindow)
  44. {
  45.     GrafPtr                 aSavedPort;
  46.     
  47.     GetPort(&aSavedPort);
  48.     SetPort(theWindow);
  49.     
  50.     // Update the SG port assuming it's in use.
  51.     if(gSG != NULL)
  52.          SGIdle(gSG);
  53.     
  54. // @@@INSERT ANY IDLE BASED FUNCTIONALITY HERE
  55.  
  56.     SetPort(aSavedPort);
  57. }
  58.  
  59.  
  60. // ______________________________________________________________________
  61. void DoUpdateWindow(WindowRef theWindow, Rect *theRefrehArea)
  62. {
  63.     GrafPtr aSavedPort;
  64.     
  65.     GetPort(&aSavedPort);
  66.     SetPort(theWindow);
  67.     
  68.     BeginUpdate(theWindow);
  69.  
  70.     if(gSG != NULL)
  71.         SGUpdate(gSG, NULL);
  72.         
  73. // @@@INSERT WINDOW SPECIFIC DRAWING FUNCTIONALITY HERE
  74.  
  75.     EndUpdate(theWindow);
  76.     SetPort(aSavedPort);
  77. }
  78.  
  79.  
  80.  
  81. // ______________________________________________________________________
  82. void DoCloseWindow(WindowRef theWindow)
  83. {
  84.     OSErr anErr = noErr;
  85.     long nTicks = 0L;
  86.     long finalTicks = TickCount(); 
  87.     float  result;
  88.  
  89.     // Clean up the channels (CloseComponent will handle that).
  90.     anErr = CloseComponent(gSG);  DebugAssert(anErr != noErr);
  91.     gSG = NULL; gVideoChannel = NULL; gAudioChannel = NULL;
  92.     gCaptureWindow = NULL;
  93.  
  94.     // Show any statistics generated while we digitized (weird code but I had problems with float conversions)
  95.     nTicks = finalTicks - gGrabCompleteTimer;            // 1/60:th of a second units
  96.     result = (float) gGrabCompleteCounter/nTicks ;
  97.     printf("The GrabFrameComplete callback was called %7.2f times a second.\n", result * 60.0 );
  98. }
  99.  
  100.  
  101. // ______________________________________________________________________
  102. void     DoDragWindow(WindowRef theWindow, EventRecord *theEvent)
  103. {
  104.     GrafPtr aSavedPort;
  105.     ICMAlignmentProcRecord alignProc;
  106.     
  107.     GetPort(&aSavedPort);
  108.     SetPort(theWindow);
  109.     
  110.     if(gSG != NULL)
  111.     {
  112.         SGPause(gSG, TRUE);
  113.         SGGetAlignmentProc(gSG, &alignProc);
  114.         
  115.         DragAlignedWindow(theWindow, theEvent->where, &qd.screenBits.bounds, NULL, &alignProc);
  116.         SGPause(gSG, FALSE);
  117.     }
  118.     // @@@INSERT WINDOW DRAGGING SPECIFIC FUNCTIONALITY HERE
  119.         
  120.     SetPort(aSavedPort);
  121. }
  122.  
  123. // ______________________________________________________________________
  124. void HandleContentClick(WindowRef theWindow, EventRecord *theEvent)
  125. {
  126.     GrafPtr aSavedPort;
  127.     
  128.     GetPort(&aSavedPort);
  129.     SetPort(theWindow);
  130.  
  131.     // @@@INSERT APPLICATION SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  132.  
  133.     SetPort(aSavedPort);
  134. }
  135.  
  136.  
  137.  
  138. // ______________________________________________________________________
  139. WindowRef CreateMovieWindow(Rect *theRect, Str255 theTitle)
  140. {
  141.     WindowPtr    aWindow     = NULL;
  142.     Rect                devRect     = {0, 0, 0, 0};
  143.     
  144.         GetBestDeviceRect(NULL, &devRect);
  145.         
  146.         OffsetRect(theRect, devRect.left + 20, devRect.top + 20);
  147.         aWindow = NewCWindow( NULL, theRect, theTitle, TRUE, noGrowDocProc, (WindowPtr)-1,
  148.                                                 TRUE, 0);
  149.     return aWindow;
  150. }
  151.  
  152. // ______________________________________________________________________
  153. void HandleApplicationMenu(short theMenuID, short theMenuItem)
  154. {
  155.     //  HANDLE ANY ADDITIONAL MENU ENTRIES HERE
  156.     switch(theMenuID)
  157.     {
  158.         // Test menus.
  159.         case mTesting:
  160.             switch(theMenuItem)
  161.             {
  162.                 case iTest1:
  163.                 {
  164.                     ShowVDIGInfo();
  165.                     break;
  166.                 }
  167.                 
  168.                 case iTest2:
  169.                 {
  170.                     SetMyVideoChannelSettings();
  171.                     break;
  172.                 }
  173.                 
  174.                 case iTest3:
  175.                 {
  176.                     SetMyAudioChannelSettings();
  177.                     break;
  178.                 }
  179.                 
  180.                 case iTest5:                // Record to file.
  181.                 {
  182.                     RecordSamplesToFile();
  183.                     break;
  184.                 }
  185.             }
  186.             break;
  187.         
  188.         // Capture Size Menus.
  189.         case mCaptureSize:
  190.         {
  191.             switch(theMenuItem)
  192.             {
  193.                 case iSizeNormal:
  194.                 {
  195.                     if( QTUChangeSGWindowSize( GetDefaultSGInstance(), GetDefaultVideoChannel() , gCaptureWindow, 320L, 240L) != noErr)
  196.                         SysBeep(kDefaultSysBeep);
  197.                     break;
  198.                 }
  199.                 
  200.                 case iSizeSmall:
  201.                 {
  202.                     if ( QTUChangeSGWindowSize( GetDefaultSGInstance(), GetDefaultVideoChannel() , gCaptureWindow,160L, 120L) != noErr)
  203.                             SysBeep(kDefaultSysBeep);
  204.                     break;
  205.                 }
  206.                 
  207.                 case iSizeBig:
  208.                 {
  209.                     if ( QTUChangeSGWindowSize( GetDefaultSGInstance(), GetDefaultVideoChannel() , gCaptureWindow, 640L, 480L) != noErr)
  210.                         SysBeep(kDefaultSysBeep);
  211.                     break;
  212.                 }
  213.             }
  214.         break;
  215.         }
  216.         
  217.         // End testing menus
  218.     }
  219. }
  220.  
  221.  
  222. // ______________________________________________________________________
  223. void    AdjustApplicationMenus(void)
  224. {
  225.     MenuHandle mHandle = NULL;
  226.     
  227.     // For the time being always dim the edit entries (maybe later if needed enable these)
  228.     mHandle = GetMHandle(mEdit);
  229.     DisableItem(mHandle, iUndo);
  230.     DisableItem(mHandle, iCut);
  231.     DisableItem(mHandle, iCopy);
  232.     DisableItem(mHandle, iPaste);
  233.     DisableItem(mHandle, iClear);
  234.     DisableItem(mHandle, iSelectAll);
  235.     
  236.     // Test if we have a valid capture window open, if true, then enable the right menus, otherwise
  237.     // disable them.
  238.     if(gCaptureWindow != NULL)
  239.     {
  240.         DisableItem(GetMHandle(mFile), iNew);
  241.         EnableItem(GetMHandle(mFile), iClose);
  242.     
  243.         mHandle = GetMHandle(mTesting);
  244.         EnableItem(mHandle, iTest1);
  245.         EnableItem(mHandle, iTest2);
  246.         EnableItem(mHandle, iTest3);
  247.         EnableItem(mHandle, iTest5);
  248.  
  249.         mHandle = GetMHandle(mCaptureSize);
  250.         EnableItem(mHandle, iSizeNormal);
  251.         EnableItem(mHandle, iSizeSmall);
  252.         EnableItem(mHandle, iSizeBig);
  253.     }
  254.     else
  255.     {
  256.         EnableItem(GetMHandle(mFile), iNew);
  257.         DisableItem(GetMHandle(mFile), iClose);
  258.  
  259.         mHandle = GetMHandle(mTesting);
  260.         DisableItem(mHandle, iTest1);
  261.         DisableItem(mHandle, iTest2);
  262.         DisableItem(mHandle, iTest3);
  263.         DisableItem(mHandle, iTest5);
  264.  
  265.         mHandle = GetMHandle(mCaptureSize);
  266.         DisableItem(mHandle, iSizeNormal);
  267.         DisableItem(mHandle, iSizeSmall);
  268.         DisableItem(mHandle, iSizeBig);
  269.     }
  270. }
  271.  
  272.  
  273. // ______________________________________________________________________
  274. void CreateSGEnviroment(void)
  275. {
  276.     OSErr                        anErr = noErr;
  277.     WindowPtr                 aWin = NULL;
  278.     Rect                            defRect = {20, 20, 260, 340};
  279.     
  280.     if(gSG != NULL || gCaptureWindow != NULL)    // already in use
  281.         return;
  282.     
  283.     gCaptureWindow = CreateMovieWindow(&defRect, "\pSG Grabber Window");
  284.     DebugAssert(gCaptureWindow != NULL);
  285.     
  286.     gSG = QTUCreateSequenceGrabber(gCaptureWindow); DebugAssert(sg != NULL);
  287.     if(gSG == NULL) 
  288.     {
  289.         printf("ERROR: Problem opening the default Sequence Grabber component.\n");
  290.         goto Closure;
  291.     }
  292.     
  293.     anErr = QTUCreateSGGrabChannels(gSG, &gCaptureWindow->portRect, seqGrabPlayDuringRecord + seqGrabRecord,
  294.                          &gVideoChannel, &gAudioChannel); DebugAssert(anErr == noErr); DebugAssert(anErr != noErr);
  295.     if(anErr != noErr) 
  296.     {
  297.         printf("ERROR: Problems creating the Video and Audio SG Channels: %ld.\n", anErr);    
  298.         goto Closure;
  299.     }            
  300.  
  301. #ifdef BOTTLENECKS    
  302.      anErr = SetupVideoBottleNecks(gVideoChannel, gCaptureWindow,  &tempPort); DebugAssert(anErr == noErr);
  303.      if(anErr != noErr)
  304.      {
  305.          printf("ERROR: Problems installing the new video bottle necks: %ld.\n", anErr);
  306.          goto Closure;
  307.      }
  308.      else
  309.          printf("STATUS: Installed video bottle necks.\n");
  310. #endif // BOTTLENECKS
  311.  
  312.     anErr = SGStartPreview(gSG); DebugAssert(anErr == noErr);
  313.     if(anErr != noErr) 
  314.     {
  315.         printf("ERROR: Problems starting the SG Preview: %ld.\n", anErr);
  316.         goto Closure;
  317.     }
  318.     else
  319.         printf("STATUS: Opened Default SG component, created channels (audio, sound), started Preview.\n");
  320.  
  321. Closure:
  322.     return;
  323. }
  324.  
  325.  
  326.  
  327. // ______________________________________________________________________
  328. SeqGrabComponent    GetDefaultSGInstance(void)
  329. {
  330.     return gSG;
  331. }
  332.  
  333.  
  334. SGChannel GetDefaultVideoChannel(void)
  335. {
  336.     return gVideoChannel;
  337. }
  338.  
  339.  
  340. SGChannel GetDefaultAudioChannel(void)
  341. {
  342.     return gAudioChannel;
  343. }
  344.  
  345.  
  346. pascal ComponentResult SpecialGrabFrameComplete(SGChannel c, short bufferNum,
  347.                                                                                 Boolean *done, long refCon)
  348. {
  349.     ComponentResult anErr = noErr;
  350.  
  351.     anErr = SGGrabFrameComplete(c, bufferNum, done); DebugAssert(anErr == noErr);
  352.     
  353.     if(gGrabCompleteCounter != 0)
  354.         gGrabCompleteCounter++;
  355.     else
  356.     {
  357.         gGrabCompleteTimer = TickCount();
  358.         gGrabCompleteCounter++;
  359.     }
  360.         
  361.     if(*done) {    // Frame is done?
  362.         CGrafPtr            tempPort = (CGrafPtr)refCon;
  363.  
  364. #if DRAWING    
  365.         Rect                    bufferRect;
  366.         GDHandle            saveGD;
  367.         PixMapHandle     savePM;
  368.         CGrafPtr            aSavedPort;
  369.         PixMapHandle    bufferPM;
  370.  
  371.         GetGWorld(&aSavedPort, &saveGD);
  372.         SetGWorld(tempPort, NULL);
  373.         
  374.         anErr = SGGetBufferInfo(c, bufferNum, &bufferPM, &bufferRect, NULL, NULL);
  375.         DebugAssert(anErr == noErr);
  376.         
  377.         if(!anErr) {
  378.             
  379.             savePM = tempPort->portPixMap;
  380.             SetPortPix(bufferPM);
  381.             
  382.             TextMode(srcXor);
  383.             MoveTo(bufferRect.right - 20, bufferRect.bottom - 14);
  384.             DrawString("\pHello");
  385.             TextMode(srcOr);
  386.             SetPortPix(savePM);
  387.         }
  388.         SetGWorld(aSavedPort, saveGD);
  389. #endif // DRAWING
  390.     }
  391.     return anErr;
  392. }
  393.  
  394.  
  395.  
  396. OSErr SetupVideoBottleNecks(SGChannel videoChannel, WindowPtr theWindow, CGrafPtr tempPort)
  397. {
  398.     OSErr anErr = noErr;
  399.  
  400.     OpenCPort(tempPort);
  401.     SetRectRgn(tempPort->visRgn, -32000, -32000, 32000, 32000);
  402.     CopyRgn(tempPort->visRgn, tempPort->clipRgn);
  403.     PortChanged((GrafPtr)tempPort);
  404.     
  405.     anErr = SGSetChannelRefCon(videoChannel, (long)tempPort); DebugAssert(anErr == noErr);
  406.     if(!anErr){
  407.         VideoBottles vb;
  408.         
  409.         vb.procCount = 9;
  410.         anErr = SGGetVideoBottlenecks(videoChannel, &vb); DebugAssert(anErr == noErr);
  411.         if(!anErr){
  412.             gSGGrabCompleteUPP = NewSGGrabCompleteProc(SpecialGrabFrameComplete);
  413.             vb.grabCompleteProc = gSGGrabCompleteUPP;
  414.             
  415.             anErr = SGSetVideoBottlenecks(videoChannel, &vb); DebugAssert(anErr == noErr);
  416.         }
  417.         
  418.     gGrabCompleteCounter = 0L;    // Setup the timer counter value.
  419.     }
  420.     return anErr;
  421. }
  422.  
  423.  
  424.